package cn.edu.nwpu.warehousemanagement.controller;

import cn.edu.nwpu.warehousemanagement.pojo.Freight;
import cn.edu.nwpu.warehousemanagement.pojo.InventoryRecord;
import cn.edu.nwpu.warehousemanagement.pojo.dto.BatchIdDTO;
import cn.edu.nwpu.warehousemanagement.pojo.dto.IDDTO;
import cn.edu.nwpu.warehousemanagement.pojo.dto.InventoryRecordQueryDTO;
import cn.edu.nwpu.warehousemanagement.pojo.dto.PageDTO;
import cn.edu.nwpu.warehousemanagement.pojo.Result;
import cn.edu.nwpu.warehousemanagement.service.InventoryRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存记录控制器
 * @author LSR
 */
@RestController
@RequestMapping("/api/inventory-records")
@RequiredArgsConstructor
public class InventoryRecordController {

    private final InventoryRecordService inventoryRecordService;

    /**
     * 分页获取未删除的库存记录
     * @param pageDTO 分页参数
     * @return 分页结果
     */
    @PostMapping("/get-inventory-records/page")
    public Result<?> getInventoryRecordsByPage(@RequestBody PageDTO pageDTO) {
        return inventoryRecordService.getInventoryRecordsByPage(pageDTO);
    }

    @PostMapping("/get-inbound-inventory-records/page")
    public Result<Page<InventoryRecord>> getInboundInventoryRecordsByPage(@RequestBody PageDTO pageDTO) {
        return inventoryRecordService.getInboundInventoryRecordsByPage(pageDTO);
    }

    @PostMapping("/get-outbound-inventory-records/page")
    public Result<Page<InventoryRecord>> getOutboundInventoryRecordsByPage(@RequestBody PageDTO pageDTO) {
        return inventoryRecordService.getOutboundInventoryRecordsByPage(pageDTO);
    }

    /**
     * 根据ID获取未删除的库存记录
     * @param id 包含记录ID的DTO
     * @return 库存记录
     */
    @PostMapping("/get-by-id")
    public Result<InventoryRecord> getInventoryRecordById(@RequestBody IDDTO id) {
        return inventoryRecordService.getInventoryRecordById(id.getId());
    }

    /**
     * 根据货物ID获取批次号
     * @param freightId 包含货物ID的DTO
     * @return 批次号
     */
    @PostMapping("/get-batch-id-by-freight-id")
    public Result<String> getBatchIdByFreightId(@RequestBody IDDTO freightId) {
        return inventoryRecordService.getBatchIdByFreightId(freightId.getId());
    }

    /**
     * 根据批次ID获取关联的货物列表
     * @param batchIdDTO 批次ID DTO
     * @return 货物列表
     */
    @PostMapping("/get-freights-by-batch-id")
    public Result<List<Freight>> getFreightsByBatchId(@RequestBody BatchIdDTO batchIdDTO) {
        return inventoryRecordService.getFreightsByBatchId(batchIdDTO.getBatchId());
    }

    /**
     * 软删除库存记录
     * @param id 包含记录ID的DTO
     * @return 操作结果
     */
    @PostMapping("/delete")
    public Result<InventoryRecord> deleteInventoryRecord(@RequestBody IDDTO id) {
        return inventoryRecordService.deleteInventoryRecord(id.getId());
    }

    /**
     * 根据批次ID获取库存记录
     * @param batchIdDTO 包含批次ID的DTO
     * @return 库存记录
     */
    @PostMapping("/get-by-batch-id")
    public Result<InventoryRecord> getByBatchId(@RequestBody BatchIdDTO batchIdDTO) {
        return inventoryRecordService.getInventoryRecordByBatchId(batchIdDTO.getBatchId());
    }

    /**
     * 根据商品名称和操作类型分页查询库存记录
     * @param request 包含商品名称、操作类型和分页参数
     * @return 分页结果
     */
    @PostMapping("/get-by-product-name-and-type/page")
    public Result<Page<InventoryRecord>> getByProductNameAndTypePage(
            @RequestBody InventoryRecordQueryDTO request) {
        return inventoryRecordService.getInventoryRecordsByProductName(
                request.getProductName(),
                request.getOperationType(),
                request.getPageDTO()
        );
    }
}